package com.example.book.controller;

import com.example.book.model.UserInfo;
import com.example.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //登录
    @RequestMapping("/login")
    public String login(String userName, String password, HttpSession session){
        log.info("接收到参数 userName: {}, password: {}", userName, password);
        //参数校验
        if(!StringUtils.hasLength(userName)
                || !StringUtils.hasLength(password)){
            return "用户名或密码为空!";
        }
        //查询是否存在数据
        UserInfo userInfo = userService.selectUserInfo(userName);
        if(userInfo == null){
            return "用户不存在!";
        }
        //判断密码是否正确
        if(!password.equals(userInfo.getPassword())){
            return "密码错误!";
        }
        //正确
        session.setAttribute("userinfo",userInfo);
        return "";
    }
}
